﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gmis.IDAL;
using Gmis.DBUtility;//Please add references
namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:Affairs
    /// </summary>
    public partial class Affairs : IAffairs
    {
        public Affairs()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Affairs");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.Affairs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Affairs(");
            strSql.Append("sId,sNumber,sTheme,dCreateDate,sPlace,sOperator,sCompere,sObject,sDelegate,sSummary,sEffect)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sNumber,@sTheme,@dCreateDate,@sPlace,@sOperator,@sCompere,@sObject,@sDelegate,@sSummary,@sEffect)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sTheme", SqlDbType.NVarChar,128),
					new SqlParameter("@dCreateDate", SqlDbType.DateTime),
					new SqlParameter("@sPlace", SqlDbType.NVarChar,50),
					new SqlParameter("@sOperator", SqlDbType.NVarChar,50),
					new SqlParameter("@sCompere", SqlDbType.NVarChar,50),
					new SqlParameter("@sObject", SqlDbType.Text),
					new SqlParameter("@sDelegate", SqlDbType.Text),
					new SqlParameter("@sSummary", SqlDbType.Text),
					new SqlParameter("@sEffect", SqlDbType.Text)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sNumber;
            parameters[2].Value = model.sTheme;
            parameters[3].Value = model.dCreateDate;
            parameters[4].Value = model.sPlace;
            parameters[5].Value = model.sOperator;
            parameters[6].Value = model.sCompere;
            parameters[7].Value = model.sObject;
            parameters[8].Value = model.sDelegate;
            parameters[9].Value = model.sSummary;
            parameters[10].Value = model.sEffect;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.Affairs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Affairs set ");
            strSql.Append("sNumber=@sNumber,");
            strSql.Append("sTheme=@sTheme,");
            strSql.Append("dCreateDate=@dCreateDate,");
            strSql.Append("sPlace=@sPlace,");
            strSql.Append("sOperator=@sOperator,");
            strSql.Append("sCompere=@sCompere,");
            strSql.Append("sObject=@sObject,");
            strSql.Append("sDelegate=@sDelegate,");
            strSql.Append("sSummary=@sSummary,");
            strSql.Append("sEffect=@sEffect");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sTheme", SqlDbType.NVarChar,128),
					new SqlParameter("@dCreateDate", SqlDbType.DateTime),
					new SqlParameter("@sPlace", SqlDbType.NVarChar,50),
					new SqlParameter("@sOperator", SqlDbType.NVarChar,50),
					new SqlParameter("@sCompere", SqlDbType.NVarChar,50),
					new SqlParameter("@sObject", SqlDbType.Text),
					new SqlParameter("@sDelegate", SqlDbType.Text),
					new SqlParameter("@sSummary", SqlDbType.Text),
					new SqlParameter("@sEffect", SqlDbType.Text),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sNumber;
            parameters[1].Value = model.sTheme;
            parameters[2].Value = model.dCreateDate;
            parameters[3].Value = model.sPlace;
            parameters[4].Value = model.sOperator;
            parameters[5].Value = model.sCompere;
            parameters[6].Value = model.sObject;
            parameters[7].Value = model.sDelegate;
            parameters[8].Value = model.sSummary;
            parameters[9].Value = model.sEffect;
            parameters[10].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Affairs ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Affairs ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.Affairs GetModel(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sId,sNumber,sTheme,dCreateDate,sPlace,sOperator,sCompere,sObject,sDelegate,sSummary,sEffect from Affairs ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.Affairs model = new Gmis.Model.Affairs();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNumber"] != null && ds.Tables[0].Rows[0]["sNumber"].ToString() != "")
                {
                    model.sNumber = ds.Tables[0].Rows[0]["sNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sTheme"] != null && ds.Tables[0].Rows[0]["sTheme"].ToString() != "")
                {
                    model.sTheme = ds.Tables[0].Rows[0]["sTheme"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dCreateDate"] != null && ds.Tables[0].Rows[0]["dCreateDate"].ToString() != "")
                {
                    model.dCreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["dCreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sPlace"] != null && ds.Tables[0].Rows[0]["sPlace"].ToString() != "")
                {
                    model.sPlace = ds.Tables[0].Rows[0]["sPlace"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sOperator"] != null && ds.Tables[0].Rows[0]["sOperator"].ToString() != "")
                {
                    model.sOperator = ds.Tables[0].Rows[0]["sOperator"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sCompere"] != null && ds.Tables[0].Rows[0]["sCompere"].ToString() != "")
                {
                    model.sCompere = ds.Tables[0].Rows[0]["sCompere"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sObject"] != null && ds.Tables[0].Rows[0]["sObject"].ToString() != "")
                {
                    model.sObject = ds.Tables[0].Rows[0]["sObject"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sDelegate"] != null && ds.Tables[0].Rows[0]["sDelegate"].ToString() != "")
                {
                    model.sDelegate = ds.Tables[0].Rows[0]["sDelegate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sSummary"] != null && ds.Tables[0].Rows[0]["sSummary"].ToString() != "")
                {
                    model.sSummary = ds.Tables[0].Rows[0]["sSummary"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sEffect"] != null && ds.Tables[0].Rows[0]["sEffect"].ToString() != "")
                {
                    model.sEffect = ds.Tables[0].Rows[0]["sEffect"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sId,sNumber,sTheme,dCreateDate,sPlace,sOperator,sCompere,sObject,sDelegate,sSummary,sEffect ");
            strSql.Append(" FROM Affairs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sId,sNumber,sTheme,dCreateDate,sPlace,sOperator,sCompere,sObject,sDelegate,sSummary,sEffect ");
            strSql.Append(" FROM Affairs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Affairs";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

